﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using TMDT_Nhom1A.DAO;

namespace TMDT_Nhom1A.Customer
{
    public partial class ThongTinCaNhan : System.Web.UI.Page
    {
        private static Customer_DTO customer = new Customer_DTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ConnectData();
            }
        }

        private void ConnectData()
        {
            if (Session["UserID"].ToString() != "")
            {
                var list = Customer_BUS.getCustomerInfoByUserID(int.Parse(Session["UserID"].ToString()));
                customer = list[0];
                dlInformationCustomer.DataSource = list;
                dlInformationCustomer.DataBind();
            }
        }



        protected void dlInformationCustomer_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "btEditInfor")
            {
                dlInformationCustomer.EditItemIndex = 0;
                ConnectData();
            }
            if (e.CommandName == "btCancelInfor")
            {
                dlInformationCustomer.EditItemIndex = -1;
                ConnectData();
            }
            if (e.CommandName == "btUpdateInfor")
            {
                TextBox tbnamecompany = (TextBox)e.Item.FindControl("tbnamecompany");
                TextBox tbaddresscompany = (TextBox)e.Item.FindControl("tbaddresscompany");
                TextBox tbnumebertax = (TextBox)e.Item.FindControl("tbnumebertax");
                TextBox txtTen = (TextBox)e.Item.FindControl("txtTen");
                TextBox txtEmail = (TextBox)e.Item.FindControl("txtEmail");
                TextBox txtdienthoai = (TextBox)e.Item.FindControl("txtdienthoai");
                TextBox txtdidong = (TextBox)e.Item.FindControl("txtdidong");
                TextBox tbrole = (TextBox)e.Item.FindControl("tbrole");
                TextBox txtPasswordOld = (TextBox)e.Item.FindControl("txtPasswordOld");
                TextBox txtPasswordNew = (TextBox)e.Item.FindControl("txtPasswordNew");
                customer.NameCompany = tbnamecompany.Text;
                customer.AddressCompany = tbaddresscompany.Text;
                customer.TaxCode = tbnumebertax.Text;
                customer.NamePresentation = txtTen.Text;
                customer.Email = txtEmail.Text;
                customer.Phone = txtdienthoai.Text;
                customer.Mobile = txtdidong.Text;
                customer.Role = tbrole.Text;
                Label lbInfoError = (Label)e.Item.FindControl("lbInfoError");
                if (txtPasswordOld.Text != "" && customer.Password == MyMembershipProvider.ToHashString(txtPasswordOld.Text + customer.UserName))
                {
                    if (txtPasswordNew.Text != "")
                    {
                        customer.Password = MyMembershipProvider.ToHashString(txtPasswordNew.Text + customer.UserName);
                        Customer_BUS.UpdateInformationOfCustomer(customer, 1);
                    }
                    else
                    {
                        Customer_BUS.UpdateInformationOfCustomer(customer, 0);
                    }
                    dlInformationCustomer.EditItemIndex = -1;
                    ConnectData();
                }
                if (txtPasswordOld.Text != "" && customer.Password != MyMembershipProvider.ToHashString(txtPasswordOld + customer.UserName))
                {
                    lbInfoError.Text = "Password cũ nhập sai, vui lòng nhập lại";
                    lbInfoError.Visible = true;
                }
                if (txtPasswordOld.Text == "" && txtPasswordNew.Text == "")
                {
                    Customer_BUS.UpdateInformationOfCustomer(customer, 0);
                    dlInformationCustomer.EditItemIndex = -1;
                    ConnectData();
                }
                if (txtPasswordOld.Text == "" && txtPasswordNew.Text != "")
                {
                    lbInfoError.Text = "Vui lòng nhập password cũ";
                    lbInfoError.Visible = true;
                }
                
            }

            if (e.CommandName == "btEditPassword")
            {
                e.Item.FindControl("btEditPassword").Visible = false;
                e.Item.FindControl("Panel1").Visible = true;
                //e.Item.FindControl("rvPasswordOld").Visible = true;
                //e.Item.FindControl("rvPasswordNew").Visible = true;
                //e.Item.FindControl("rvConfirmPasswordNew").Visible = true;

            }
            if (e.CommandName == "btCancelPassword")
            {
                e.Item.FindControl("btEditPassword").Visible = true;
                e.Item.FindControl("Panel1").Visible = false;
                //e.Item.FindControl("rvPasswordOld").Visible = false;
                //e.Item.FindControl("rvPasswordNew").Visible = false;
                //e.Item.FindControl("rvConfirmPasswordNew").Visible = false;
            }
        }
    }
}
